package com.example.administrator.exkotlin.multithread

import android.app.Activity
import android.os.Handler
import android.os.Message
import android.util.Log
import java.lang.ref.WeakReference

/**
 * Author by lp,on 2020/8/5/005,16:55.
 */
class MutiHandler(val weakReference: WeakReference<Activity>) :Handler() {
    private val TAG="MultithreadActivity"
    override fun handleMessage(msg: Message) {
        super.handleMessage(msg)
        val weak=weakReference.get() as MultithreadActivity
        if (weak == null) {
            removeMessages(msg.what)
            Log.i(TAG, "Activity被回收了")
            return
        }
        when (msg.what) {
            weak.msg1 -> {
                val result = weak.updataUi()
                if (result) {
                    sendEmptyMessageDelayed(msg.what, weak.delayTime)
                } else {
                    removeMessages(msg.what)
                }
            }

        }
    }
}